package com.yixun.qfbao.service;

import com.yixun.qfbao.controller.vo.SmsHistoryVo;
import com.yixun.qfbao.model.SmsHistory;
import com.yixun.qfbao.util.DateStyle;
import com.yixun.system.utils.DateUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ExportService {
	private static Logger logger= LoggerFactory.getLogger(ExportService.class);

	
	public static void exportSmsHistory(List<SmsHistory> smsHisList, SmsHistoryVo entity, HttpServletResponse response) {
		try {
			  @SuppressWarnings("resource")
			  HSSFWorkbook wb = new HSSFWorkbook();
			  HSSFSheet sheet = wb.createSheet(); 
			  sheet.createFreezePane( 0, 1, 0, 1 );
			  //合并第一行
			  CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 3);
			  sheet.addMergedRegion(cra);
			  //合并第二行
			  cra = new CellRangeAddress(1, 1, 1, 3);
			  sheet.addMergedRegion(cra);
			  //合并第三行
			  cra = new CellRangeAddress(2, 2, 1, 3);
			  sheet.addMergedRegion(cra);
		      wb.setSheetName(0, "短信发送记录");
		      
			  HSSFCellStyle headStyle = wb.createCellStyle(); 
			  HSSFFont f  = wb.createFont();    
			  f.setFontHeightInPoints((short) 15);// 字号   
		      f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗   
		      headStyle.setFont(f); 
		      headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
		      headStyle.setBorderBottom((short) 1);
		      headStyle.setBorderRight((short) 1);
		      headStyle.setFillBackgroundColor((short) 20);
		      
		      HSSFRow row = sheet.createRow(0); 
		      //创建单元格，并设置值表头 设置表头居中  
		      HSSFCellStyle style = wb.createCellStyle();
		      style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
		      style.setBorderBottom((short) 1);
		      style.setBorderRight((short) 1);
		      
		      HSSFCell cell = row.createCell(0);
		      cell.setCellStyle(style);
		      HSSFRichTextString text = new HSSFRichTextString("渠道管理短信使用汇总表");
		      cell.setCellValue(text);
		      
		      //第二行商户名
		      row = sheet.createRow(1);
		      style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		      cell = row.createCell(0);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("商户名：");
		      cell.setCellValue(text);
		      style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		      cell = row.createCell(1);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString(entity.getCompanyName());
		      cell.setCellValue(text);
		      
		      //第三行账期
		      row = sheet.createRow(2); 
		      //创建单元格，并设置值表头 设置表头居中  
		      style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); 
		      cell = row.createCell(0);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("账期：");
		      cell.setCellValue(text);
		      style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		      cell = row.createCell(1);
		      cell.setCellStyle(style);
		      String timeStr = "";
		      if(null != entity.getCreateTimeBefore()) {
		    	  timeStr = DateUtil.dateToString(entity.getCreateTimeBefore(), DateStyle.YYYY_MM_DD.getValue());
		      }else {
		    	  timeStr = "~";
		      }
		      timeStr += " 至 ";
		      if(null != entity.getCreateTimeAfter()) {
		    	  timeStr += DateUtil.dateToString(entity.getCreateTimeAfter(), DateStyle.YYYY_MM_DD.getValue());
		      }else {
		    	  timeStr += "~";
		      }
		      text = new HSSFRichTextString(timeStr);
		      cell.setCellValue(text);
		      
		      //第四行数量统计
		      row = sheet.createRow(3);
		      //创建单元格，并设置值表头 设置表头居中  
		      style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); 
		      cell = row.createCell(0);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("短信数量总计：");
		      cell.setCellValue(text);
		      style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		      cell = row.createCell(1);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString(String.valueOf(smsHisList.size()));
		      cell.setCellValue(text);
		      
		      row = sheet.createRow(4);
		      style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
		      cell = row.createCell(0);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("序号");
		      cell.setCellValue(text);
		      cell = row.createCell(1);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("类型");
		      cell.setCellValue(text);
		      cell = row.createCell(2);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("发送目标号码");
		      cell.setCellValue(text);
		      cell = row.createCell(3);
		      cell.setCellStyle(style);
		      text = new HSSFRichTextString("发送时间");
		      cell.setCellValue(text);
		      
		      if(null != smsHisList && smsHisList.size() > 0) {
		    	  for(int ii=0; ii<smsHisList.size(); ii++) {
		    		  SmsHistory smsHis = smsHisList.get(ii);
		    		  row = sheet.createRow(ii + 5);
		    		  cell = row.createCell(0);
				      cell.setCellStyle(style);
				      text = new HSSFRichTextString(String.valueOf(ii+1));
				      cell.setCellValue(text);
				      
				      cell = row.createCell(1);
				      cell.setCellStyle(style);
				      text = new HSSFRichTextString(smsHis.getRemark());
				      cell.setCellValue(text);
				      
				      cell = row.createCell(2);
				      cell.setCellStyle(style);
				      text = new HSSFRichTextString(smsHis.getReceiver());
				      cell.setCellValue(text);
				      
				      cell = row.createCell(3);
				      cell.setCellStyle(style);
				      text = new HSSFRichTextString(DateUtil.dateToString(smsHis.getCreateTime(), DateStyle.YYYY_MM_DD.getValue()));
				      cell.setCellValue(text);
		    	  }
		      }

			 SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			 String downFileName = entity.getCompanyName()+"_"+df.format(new Date()) + ".xls";
			 downFileName = URLEncoder.encode(downFileName, "UTF-8"); 
			 response.reset();
			 response.setCharacterEncoding("utf-8");
			 response.setHeader("Content-Disposition","attachment;filename="+downFileName);
			 response.setContentType("application/vnd.ms-excel;charset=utf-8");
			 OutputStream out = response.getOutputStream();
			 wb.write(out);
			 out.close();
			 out.flush();
			 out.close();
		} catch (Exception e) {
			logger.error("exportSmsHistory error: {}", e);
		}
		
	} 
}
